//
//  ChoseDateView.m
//  QinghaiTourism
//
//  Created by xujingjing on 16/6/15.
//  Copyright © 2016年 xujingjing. All rights reserved.
//

#define  btnWidth  50  //查询按钮的宽度
#define  DATEPICHIGHT  200  //datepic的宽度

#import "ChoseDateView.h"
#import "UIView+FindNav.h"//获取父控制器


@interface  ChoseDateView()
{
        
        UIButton *whichTime;
        NSString *dateAndTime;
        UIView *viewBlack;
        UITapGestureRecognizer *_tap;
        
}

@end
@implementation ChoseDateView

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self interface]; //两个时间按钮和查询按钮
        [self addPickerView]; //取消 确定 和datePickerView

        
        _tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(dismissviewTime)];
        //    _tap.numberOfTapsRequired = 1;
        
    
        [self.navgationController.view addGestureRecognizer:_tap];
        

        
        
    }
    return self;
}
-(void)interface{
    
    [self addSubview:self.startBtn];
    [self.layer addSublayer:self.layerLine];
    [self addSubview:self.endBtn];
    [self addSubview:self.buttonSearch];
    
    
}

#pragma mark buttonPressed
-(void)buttonPressed:(UIButton *)sender
{
    _viewTime.hidden = NO;
    whichTime= sender;
    [_viewTime addSubview:_datePicker];
  //  [self.navigationController.view addSubview:_viewTime];
   // [[UIApplication sharedApplication].keyWindow addSubview:_viewTime];
    [self endEditing:YES];
   
    [self dateChanged:_datePicker.date];//设置没有选择就显示今天的日期
    
    
}
-(void)dismissviewTime{
    
    _viewTime.hidden = YES;
    
    [_viewTime removeFromSuperview];
    [self.navgationController.view removeGestureRecognizer:_tap];
    
}

-(void)dateChanged:(UIDatePicker *)sender{
    //date->string
    NSDate *pickerDate = _datePicker.date;
    dateAndTime =  [self stringFromDate:pickerDate];
    
    
    NSLog(@"--%@",dateAndTime);
}
//date - >string
-(NSString *)stringFromDate:(NSDate *)date{
   
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];
    return [dateFormatter stringFromDate:date];
}

//cancel
- (void)cancelButton:(UIButton *)sender {
    [self dismissviewTime];
}
//ok
- (void)okButton:(UIButton *)sender {
    
    [whichTime setTitle:dateAndTime forState:UIControlStateNormal];
    
    if (![_startBtn.currentTitle isEqualToString:@"不限"] && ![_endBtn.currentTitle isEqualToString:@"不限"]) {
        
        if ([self timetoNumber:_startBtn.currentTitle] >[self timetoNumber:_endBtn.currentTitle]  ) {
            
            [[[UIAlertView alloc]initWithTitle:@"温馨提示" message:@"选择时间不正确！" delegate:self cancelButtonTitle:nil otherButtonTitles:@"确定", nil] show];
          
            _startBtn.titleLabel.text = @"不限";
            _endBtn.titleLabel.text = @"不限";

            
        }
        
    }
    [self dismissviewTime];
    
}

-(NSInteger)timetoNumber:(NSString *)dateAndTimeee{
    //sting->date->nsinteger
    NSDateFormatter *formatter = [[NSDateFormatter alloc]init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    NSDate *date = [formatter dateFromString:dateAndTimeee];
    
    NSInteger num = [date timeIntervalSince1970];
    NSLog(@"num%ld",num);
    return num;
    
}






#pragma mark getter && setter

-(UIButton *)startBtn{
    if (!_startBtn) {
        
        _startBtn =[UIButton buttonWithType:UIButtonTypeCustom];
        
        _startBtn.frame = CGRectMake(0, 0, (kWindowWidth - btnWidth)/2, CGRectGetHeight(self.frame));
        
        [_startBtn setImage:[UIImage imageNamed:@"我的_财务_03"] forState:UIControlStateNormal];
        [_startBtn setTitle:@"不限" forState:UIControlStateNormal];
        
                    CGFloat imageWidth = _startBtn.imageView.bounds.size.width;
                    _startBtn.imageEdgeInsets = UIEdgeInsetsMake(0,0, 0, imageWidth/2);
                    _startBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -imageWidth/2);

        
        [_startBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        _startBtn.titleLabel.textAlignment = NSTextAlignmentRight;
        [_startBtn addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _startBtn;
}


-(CALayer *)layerLine{
   
    if (!_layerLine) {
        _layerLine = [CALayer layer];
        _layerLine.frame = CGRectMake(CGRectGetWidth(_startBtn.frame), 10, 0.5,CGRectGetHeight(_startBtn.frame) - 20);
        _layerLine.backgroundColor = [UIColor grayColor].CGColor;
        

        
    }
    return _layerLine;

}
-(UIButton *)buttonSearch{
    if (!_buttonSearch) {
        _buttonSearch =[UIButton buttonWithType:UIButtonTypeCustom];
        _buttonSearch.frame = CGRectMake(kWindowWidth-btnWidth,0 , btnWidth, CGRectGetHeight(self.frame));
  
        [_buttonSearch setTitle:@"查询" forState:UIControlStateNormal];
        [_buttonSearch setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_buttonSearch setBackgroundColor:[UIColor orangeColor]];
        
        
    }
    return _buttonSearch;
}
-(UIButton *)endBtn{
    if (!_endBtn) {
        _endBtn =[UIButton buttonWithType:UIButtonTypeCustom];
        _endBtn.frame = CGRectMake(CGRectGetWidth(_startBtn.frame), 0, CGRectGetWidth(_startBtn.frame), CGRectGetHeight(self.frame));
        
        [_endBtn setImage:[UIImage imageNamed:@"我的_财务_03"] forState:UIControlStateNormal];
            [_endBtn setTitle:@"不限" forState:UIControlStateNormal];
        CGFloat imageWidth = _endBtn.imageView.bounds.size.width;
        
        _endBtn.imageEdgeInsets = UIEdgeInsetsMake(0,0, 0, imageWidth/2);
        _endBtn.titleEdgeInsets = UIEdgeInsetsMake(0, 0, 0, -imageWidth/2);
        [_endBtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
        [_endBtn addTarget:self action:@selector(buttonPressed:) forControlEvents:UIControlEventTouchUpInside];
        

    }
    return _endBtn;

}
-(void)addPickerView{
    _viewTime  = [[UIView alloc]initWithFrame:CGRectMake(0, SCREENH_HEIGHT - DATEPICHIGHT, kWindowWidth,DATEPICHIGHT+40)];
    _viewTime.backgroundColor = [UIColor lightGrayColor];
    
    UIButton *cancelButton = [UIButton buttonWithType:UIButtonTypeSystem];
    cancelButton.frame = CGRectMake(0, 0, 80, 40);
    [cancelButton setTitle:@"取消" forState:UIControlStateNormal];
    [cancelButton addTarget:self action:@selector(cancelButton:) forControlEvents:UIControlEventTouchUpInside];
    [_viewTime addSubview:cancelButton];
    
    UIButton *okButton = [UIButton buttonWithType:UIButtonTypeSystem];
    okButton.frame = CGRectMake(kWindowWidth-80, 0, 80, 40);
    [okButton setTitle:@"确定" forState:UIControlStateNormal];
    [okButton addTarget:self action:@selector(okButton:) forControlEvents:UIControlEventTouchUpInside];
    [_viewTime addSubview:okButton];
    
    _datePicker = [[UIDatePicker alloc]initWithFrame:CGRectMake(0, 40, kWindowWidth, DATEPICHIGHT)];
    [_datePicker addTarget:self action:@selector(dateChanged:) forControlEvents:UIControlEventValueChanged];
    
    
    
}

@end
